
export default  class  WebRoot  {

    constructor(rootPath){

        this.cache={}; //模块
        this.cacheConfig={} ;
        this.rootPath=rootPath;
    }
    //直接从缓存中获取数据
    findCacheData(cacheKey){
        let _this=this;
        return  _this.cache[cacheKey]
    }
    //获取模块
    findData(moduleName,dir,fsName,loadName ){
        let _this=this;
        let configCache = this.cacheConfig[loadName]
        if(!configCache){
            // let cacheKey=configCache.findKey(moduleName,dir,fsName,loadName);
            return new Promise(function (resolve, reject) {
                if(_this.cache[moduleName]){

                    resolve(_this.cache[moduleName]);
                }else {
                    let res= _this.load(moduleName,dir,fsName,loadName )

                    res.then(resData =>{
                        _this.cache[moduleName]=resData;

                        resolve(resData);
                    })
                }
            });
        }else {
            return new Promise(function (resolve, reject) {
                let res= _this.load(moduleName,dir,fsName,loadName)

                res.then(resData =>{
                    _this.cacheConfig
                    resolve(resData);
                })
            });
        }
    }

    configCache(configCache,dir,fsName,loadName){
        this.cacheConfig[loadName]=configCache;
    }
      load(dir){
          let _this=this;
        return new Promise(function(resolve, reject) {
debugger
            var xhr = new XMLHttpRequest();//创建新的XHR对象
            xhr.open('GET', _this.rootPath+"/"+dir);//指定获取数据的方式和url地址
            let blob;
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
            xhr.responseType = 'blob';//以blob的形式接收数据，一般文件内容比较大
            xhr.onload = function (e) {
                let _this=this;
                if (_this.status == 200  ){
                        var reader = new FileReader();
                        reader.onload = function () {
                            resolve(this.result)      ;//arraybuffer 数据

                        };
                        reader.readAsArrayBuffer(_this.response);//Blob数据
                }else
                {
                    throw  e;
                }


            };
            xhr.send(); //post请求传的参数
        })
    }

}
